//
//  SISSRecommandPassengerCell.m
//  SISSAPP
//
//  Created by 优房网 on 2017/3/29.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSRecommandPassengerCell.h"

@interface SISSRecommandPassengerCell ()

/**
 名字求购label
 */
@property (weak, nonatomic) UILabel *nameLabel;
/**
 详细信息label
 */
@property (weak, nonatomic) UILabel *detailLabel;
/**
 选择按钮
 */
@property (weak, nonatomic) UIButton *checkButton;
/**
 线下图片
 */
@property (weak, nonatomic) UIImageView *offlineImageView;

@end
@implementation SISSRecommandPassengerCell

- (void)setPassengerResourceModel:(SISSPassengerResourceModel *)passengerResourceModel {
    
    _passengerResourceModel = passengerResourceModel;
    
    self.nameLabel.text = [NSString stringWithFormat:@"%@  %@",passengerResourceModel.name,@"求购"];
    self.detailLabel.text = [NSString stringWithFormat:@"%0.f-%0.f万 | %@-%@ | %@",passengerResourceModel.minPrice,passengerResourceModel.maxPrice,passengerResourceModel.region,passengerResourceModel.regionDetail,passengerResourceModel.houseType];
    self.offlineImageView.hidden = passengerResourceModel.demandType == 1;
    
    self.checkButton.selected = passengerResourceModel.isSelected;
}

- (void)setIsChooseMain:(BOOL)isChooseMain {
    _isChooseMain = isChooseMain;
    
    NSString *imageName = isChooseMain ? @"mainCheckSelected" : @"checkSelected";
    [self.checkButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateSelected];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}


/**
 布局子视图
 */
- (void)setupUI {
    
    // 名字 求购
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.font = [UIFont systemFontOfSize:14];
    nameLabel.textColor = MAIN_TEXT_COLOR;
    nameLabel.text = @"陆先生 求购";
    [self.contentView addSubview:nameLabel];
    
    // 详细信息
    UILabel *detailLabel = [[UILabel alloc] init];
    detailLabel.font = [UIFont systemFontOfSize:14];
    detailLabel.textColor = MAIN_TEXT_COLOR;
    detailLabel.text = @"300-500万 | 南山区-西丽 | 3房";
    [self.contentView addSubview:detailLabel];
    
    // 选择按钮
    UIButton *checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [checkButton setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
    [checkButton setImage:[UIImage imageNamed:@"checkSelected"] forState:UIControlStateSelected];
    [checkButton addTarget:self action:@selector(checkButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [checkButton setBackgroundImage:[PublicUtil imageWithColor:[UIColor clearColor] size:CGSizeMake(60, 60)] forState:UIControlStateNormal];
    [self.contentView addSubview:checkButton];
    
    UIImageView *offlineImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"offline"]];
    [offlineImageView sizeToFit];
    offlineImageView.hidden = YES;
    [self.contentView addSubview:offlineImageView];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:lineView];
    
    // 自动布局
    WeakSelf
    CGFloat margin = 8;
    CGSize buttonSize = CGSizeMake(44, 44);
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(margin);
        make.left.equalTo(weakSelf.contentView).offset(margin * 2.5);
    }];
    
    [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLabel.mas_bottom).offset(margin * 2);
        make.left.equalTo(nameLabel);
        make.bottom.equalTo(lineView).offset(-margin);
    }];
    
    [checkButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.contentView);
        make.right.equalTo(weakSelf.contentView).offset(-margin * 2);
        make.size.mas_equalTo(buttonSize);
    }];
    
    [offlineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.contentView).offset(-margin);
        make.top.equalTo(weakSelf.contentView);
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
    
    // 记录控件
    self.nameLabel = nameLabel;
    self.detailLabel = detailLabel;
    self.checkButton = checkButton;
    self.offlineImageView = offlineImageView;
}


/**
 选择按钮点击事件
 */
- (void)checkButtonClickAction:(UIButton *)button {
    
    
    if ([self.delegate respondsToSelector:@selector(recommandPassengerCell:checkButtonIsSelected:)]) {
         BOOL doSelection = [self.delegate recommandPassengerCell:self checkButtonIsSelected:!self.checkButton.isSelected];
        
        if(!doSelection){
            
        }else{
            self.passengerResourceModel.isSelected = !self.passengerResourceModel.isSelected;
            self.checkButton.selected = self.passengerResourceModel.isSelected;
        }
    }
}

@end
